package com.handinfo.uimenu.window;

import java.io.DataInputStream;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class UIActivity extends UIAbstractWindow
{
	/**
	 *  当前状态
	 *  (0基本任务未接完)
	 * （1基本任务完成未接扩展任务）
	 * （2基本任务完成已接扩展任务）
	 * （3基本任务和扩展任务完成）
	 */
	public byte m_state = 0;
	         
	public String[] m_infos;//显示文字信息
	public String m_task_infos;//任务列表
	
	private byte m_basic_maxtask = 0;//每日任务的最大次数
    private byte m_basic_task = 0;//每日任务的已接次数
    private byte m_extension_maxtask = 0;//每日可扩展任务的最大次数
    private byte m_extension_task = 0;//每日已购买的扩展任务次数
    private byte m_option_num = 0;//可选项 
    private int m_option_complete_width = AbstractGameScreen.FONT_W * 5;
    private String m_title_task = "每日任务";
	private String m_option_buy = "购买任务";
	private String m_option_complete = "立即完成";
	private String m_task_key;//任务KEY
	private byte m_task_state;//当前任务状态  0未接 1已接
//	private boolean m_isScreen;//绘制屏锁
	
	private boolean m_select = false;//FALSE 选择在任务处 True 在OPTTION处
	
	public UIActivity(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		m_select = false;
	}
    
	public void init(){
	    StringBuffer buff = new StringBuffer();
		//基本任务中
		if( m_basic_task < m_basic_maxtask ){
			m_state = 0;
			m_option_num = 2;
			m_select = false;
			buff.append("您每日可完成"+m_basic_maxtask+"次历练任务，");
			buff.append("已完成"+m_basic_task+"次！");
			buff.append(Tools.CHANGE_ROW);
			buff.append("点击任务可查看任务内容，接受任务后才消耗次数！");	
		}else{//扩展任务中
			if( m_task_infos != null ){//有任务列表
				m_state = 2;
				m_option_num = 2;
				m_select = false;
			    buff.append("您已完成"+m_basic_task+"次历练任务及"+m_extension_task+"次额外任务，仍可购买"+(m_extension_maxtask-m_extension_task)+"次额外任务");
			}else{
				if( m_extension_task == m_extension_maxtask ){//购买次数已达上限
					m_state = 3;
					m_option_num = 0;
					m_select = false;
					buff.append("您已完成全部"+m_basic_task+"次历练任务和"+m_extension_task+"次额外任务");
				}else{
					m_state = 1;
					m_option_num = 1;
					m_select = true;
					buff.append("您每日已完成"+m_basic_task+"次历练任务，可购买最多"+m_extension_maxtask+"次额外任务");
				}
			}
		}
		m_infos = Tools.getStrArray(buff.toString(), UITools.W_RECT);
		buff = null;
	}
	
	protected void paintContent(Graphics g)
	{
		//绘制背景
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, m_title_task, false, true);
        //显示信息
		int x = m_x + UITools.INITX_CONTENT;
		int y = m_y + UITools.INITY_CONTENT;
		if( m_infos != null ){
			for(int i=0;i<m_infos.length;i++){
				if( m_infos[i] != null){
					UITools.drawColorString(g, m_infos[i], x, y+(i*UITools.H_PRE), 0xffffff, Graphics.TOP | Graphics.LEFT);
				}
			}
		}
		switch(m_state){
        	case 0:
        	case 2:
        		//显示任务框
        		int w = UITools.W_CONTENT;
        		int h = UITools.H_LABEL_STR;
        		y = m_y + UITools.INITY_CONTENT + (m_infos.length+1) * UITools.H_PRE;
        		g.setColor(0xFFFFFF);
        	    g.fillRect( x, y, w, h );
        	    g.setColor(0x0);
        	    g.drawRect( x, y, w, h );
        	    if( !m_select ){
        		    UITools.paintFocusBox(g, x - 1, y - 2, w + 2, h + 4);
        	    }
        	    //显示任务名字
        	    if( m_task_infos != null ){
        	    	x = m_x + UITools.INITX_CONTENT +((UITools.W_CONTENT - AbstractGameScreen.s_font.stringWidth(m_task_infos))>>1);
        	    	UITools.drawColorString(g, m_task_infos, x, y+2, 0x0, Graphics.TOP | Graphics.LEFT);
        	    }
        		//显示选择框
        		x = (m_x+((m_w - m_option_complete_width) >> 1));
        		y = (m_y + m_h - (UITools.H_LABEL_STR << 1));
                UITools.paintLabelStr(g, x, y, m_option_complete_width, UITools.H_LABEL_STR, m_option_complete, m_select, m_select);
        		break;
        	case 1:
        		//显示选择框
        		x = (m_x+(m_w - m_option_complete_width) >> 1);
        		y = (m_y + m_h - (UITools.H_LABEL_STR << 1));
        		 UITools.paintLabelStr(g, x, y, m_option_complete_width, UITools.H_LABEL_STR, m_option_buy, m_select, m_select);
        		break;
        }
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_option_num > 1) {
				m_select = m_select ? false : true;
			}
		}else if( AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
				||AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)){
			  switch(m_state){
				  case 0:
				  case 2:
					  if( !m_select ){
						  if( m_task_key != null ){
							  if( m_task_state == 0 ){
								  GameManager.getInstance().m_game_message.sendNPCTaskView(m_task_key);
							  }else if( m_task_state == 1 ){
								  GameManager.getInstance().m_game_message.sendRoleTaskView(m_task_key);
							  }
							  close();
						  }
					  }else{
						  GameManager.getInstance().m_game_message.sendActivityCompleteTask();  
					  }				  
					  break;
				  case 1:
					  GameManager.getInstance().m_game_message.sendActivityBuyTask();
					  break;
			  }
		}else if( AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT) ){
			close();
		}
		
	}
    
	/**
	 * 接收数据创建每日任务
	 * */
	public static final void recvActivityInfos(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvActivityInfos");
			byte state = dis.readByte();
			if(state != 1){
				String context = dis.readUTF();
				GameManager.getInstance().m_game_message.unlockNetwork();
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, context);
				return;
			}
			UIActivity  win = (UIActivity)UIWindowFactory.createWindow(UIWindowFactory.WINDOW_ID_ACTIVITY);
			win.m_basic_maxtask = dis.readByte();
			win.m_basic_task = dis.readByte();
			win.m_extension_maxtask = dis.readByte();
			win.m_extension_task = dis.readByte();
			win.m_task_state = dis.readByte();
			win.m_task_infos = dis.readUTF();
			win.m_task_key = dis.readUTF();
//		    Tools.debugPrintln("m_basic_maxtask"+win.m_basic_maxtask);
//		    Tools.debugPrintln("m_basic_task"+win.m_basic_task);
//		    Tools.debugPrintln("m_extension_maxtask"+win.m_extension_maxtask );
//		    Tools.debugPrintln("m_extension_task"+win.m_extension_task);
//		    Tools.debugPrintln("infos="+win.m_task_infos);
		    if( win.m_task_infos.length() < 1){
		    	win.m_task_infos = null;
		    	win.m_task_key = null;
		    }else{
		    	String temp = win.m_task_state == 0?"(未接)":"(已接)";
		    	win.m_task_infos.concat(temp);
		    }
		    win.init();
		    UIManager.getInstance().addWindow(win);
			dis.close();
			dis = null;
			GameManager.getInstance().m_game_message.unlockNetwork();	
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
	/**
	 * 接收立即完成任务
	 * */
	public static final void recvActivityCompleteTask(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvActivityChangeTask");
			GameManager.getInstance().m_game_message.unlockNetwork();
			String name = "立刻完成";
			String decribe = "您可以使用元宝或银票立刻完成当前每日任务，并消耗每日任务次数";
			long yinpiao = dis.readLong();
			long yuanbao = dis.readLong();
			UIActivity win = (UIActivity)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_ACTIVITY);
			String tasknameString = null;
			if( win != null ){
				tasknameString = win.m_task_infos;
			}
			UIMultiMoney.gotoBuyTaskImFin(name, tasknameString, null, 0, decribe, yinpiao, yuanbao);
			dis.close();
			dis = null;
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
	/**
	 * 接收确定立即完成任务
	 * */
	public static final void recvActivityCompleteTaskOk(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvActivityChangeTaskOk");
			    byte state = dis.readByte();
			    if( state != 1){
				    String context = dis.readUTF();
			    	GameManager.getInstance().m_game_message.unlockNetwork();
			    	GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, context);
			    	return;
			    }
				UIActivity win = (UIActivity)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_ACTIVITY);
				String conntext = "恭喜您！任务完成！";
				if( win != null ){
					win.m_basic_maxtask = dis.readByte();
					win.m_basic_task = dis.readByte();
					win.m_extension_maxtask = dis.readByte();
					win.m_extension_task = dis.readByte();
					win.m_task_state = dis.readByte();
					win.m_task_infos = dis.readUTF();
					win.m_task_key = dis.readUTF();	
				    if( win.m_task_infos.length() < 1){
				    	win.m_task_infos = null;
				    	win.m_task_key = null;
				    }else{
				    	String temp = win.m_task_state == 0?"(未接)":"(已接)";
				    	win.m_task_infos.concat(temp);
				    }
				    win.init();
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, conntext);
		            GameManager.getInstance().m_game_message.unlockNetwork();
				}else{
					throw new Exception();
				}		
            dis.close();
            dis = null;
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
	/**
	 * 接收购买任务
	 * */
	public static final void recvActivityBuyTask(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvActivityBuyTask");
			GameManager.getInstance().m_game_message.unlockNetwork();
			String name = "购买任务";
			long yinpiao = dis.readLong();
			long yuanbao = dis.readLong();
			byte num = dis.readByte();
			StringBuffer buff = new StringBuffer();
			buff.append("您可以使用<mark c=898098>");
			buff.append(yuanbao+"元宝</mark>");
			if( yinpiao > 0 ){
				buff.append("或银票");
			}
			buff.append("取得第"+num+"次每日任务机会。");
			String decribe = buff.toString();
			UIActivity win = (UIActivity)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_ACTIVITY);
			String tasknameString = null;
			if( win != null ){
				tasknameString = win.m_task_infos;
			}
			UIMultiMoney.gotoBuyTaskImBuy(name, tasknameString, null, 0, decribe, yinpiao, yuanbao);
			buff = null;
			dis.close();
			dis = null;
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
	/**
	 * 接收确定购买任务
	 * */
	public static final void recvActivityBuyTaskOk(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvActivityBuyTaskOk");
			byte state = dis.readByte();
			if( state != 1 ){
				String conntext = dis.readUTF();
				GameManager.getInstance().m_game_message.unlockNetwork();
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, conntext);
				return;
			}
				UIActivity win = (UIActivity)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_ACTIVITY);
				if( win != null ){
				    win.m_basic_maxtask = dis.readByte();
				    win.m_basic_task = dis.readByte();
				    win.m_extension_maxtask = dis.readByte();
				    win.m_extension_task = dis.readByte();
				    win.m_task_state = dis.readByte();
				    win.m_task_infos = dis.readUTF();
				    win.m_task_key = dis.readUTF();
				    if( win.m_task_infos.length() < 1){
				    	win.m_task_infos = null;
				    	win.m_task_key = null;
				    }else{
				    	String temp = win.m_task_state == 0?"(未接)":"(已接)";
				    	win.m_task_infos.concat(temp);
				    }
				    win.init();
				}
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "恭喜您！购买成功。");
            GameManager.getInstance().m_game_message.unlockNetwork();
            dis.close();
            dis = null;
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
}
